package com.agung.wonogirimaps;

import java.util.List;

import org.json.JSONObject;

import com.agung.wonogirimaps.adapter.RumahMakanAdapter;
import com.agung.wonogirimaps.entity.RumahMakan;
import com.agung.wonogirimaps.util.JSONAdapter;
import com.agung.wonogirimaps.util.Utils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;

public class RumahMakanActivity extends ActionBarActivity
{
	private ListView			lv;
	private JSONAdapter			json;
	private RumahMakanAdapter	adapter;
	private ProgressDialog		pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rumah_makan);

		lv = (ListView) findViewById(R.id.list_rumahmakan);

		getSupportActionBar().setTitle("Daftar Rumah Makan");
		getSupportActionBar().setIcon(android.R.color.transparent);

		json = new JSONAdapter();

		new AsyncRumahMakan().execute(Utils.API_RUMAH_MAKAN);

	}

	private class AsyncRumahMakan extends
			AsyncTask<String, Void, List<RumahMakan>>
	{
		@Override
		protected List<RumahMakan> doInBackground(String... arg0)
		{
			JSONObject obj = json.getJSONFromURL(arg0[0]);

			return json.getAllRumahMakan(obj);
		}

		@Override
		protected void onPostExecute(List<RumahMakan> result)
		{
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			pDialog.dismiss();

			adapter = new RumahMakanAdapter(RumahMakanActivity.this, result);
			lv.setAdapter(adapter);

		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(RumahMakanActivity.this);
			pDialog.setMessage("Loading");
			pDialog.setCancelable(true);
			pDialog.show();
		}
	}

	@Override
	public void onBackPressed()
	{
		// TODO Auto-generated method stub
		super.onBackPressed();
		startActivity(new Intent(this, MainActivity.class));
		finish();
	}

}
